package com.yun2.bms.controller;

import com.yun2.bms.Login;
import com.yun2.bms.model.User;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.fxml.Initializable;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Hyperlink;
import javafx.scene.control.Label;
import javafx.scene.layout.AnchorPane;
import javafx.scene.layout.StackPane;
import javafx.stage.Stage;

import java.io.IOException;
import java.net.URL;
import java.util.ResourceBundle;

/**
 * author: youli
 * qq: 27855162@qq.com
 * date: 2020/5/21
 */

public class MainController implements Initializable {

    private User loginUser;

    public User getLoginUser() {
        return loginUser;
    }

    public void setLoginUser(User loginUser) {
        this.loginUser = loginUser;
    }

    @FXML
    private Hyperlink hlLendBook;

    @FXML
    private Hyperlink hlUserManage;

    @FXML
    private Hyperlink hlReturnBook;

    @FXML
    private Label lblWelcome;

    @FXML
    private Hyperlink hlCategoryManage;

    @FXML
    private Hyperlink hlBookManage;

    @FXML
    private Hyperlink hlReLendBook;

    @FXML
    private StackPane mainContainer;

    @FXML
    private Hyperlink hlLogout;

    @FXML
    void e10b0b(ActionEvent event) {

    }

    //切换到用户管理界面
    @FXML
    void getUserList(ActionEvent event) {
        changeContent("user/user.fxml");
    }

    //切换到图书管理界面
    @FXML
    void getBookList(ActionEvent event) {
        changeContent("book/book.fxml");
    }

    //切换到分类管理界面
    @FXML
    void getCategoryList(ActionEvent event) {
        changeContent("category/category.fxml");
    }

    //用于切换不同的管理界面
    private void changeContent(String fxmlName) {
        AnchorPane pane = null;
        try {
            pane = FXMLLoader.load(getClass().getResource("/fxml/" + fxmlName));
            //将pane加入到main.fxml中的名字为mainContainer的StackPane中,
            //实际上就是将pane添加到mainContainer孩子节点的集合中。
            mainContainer.getChildren().clear();
            mainContainer.getChildren().add(pane);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //切换到登记借书界面
    @FXML
    void toLendBook(ActionEvent event) {
        changeContent("lend/lend.fxml");
    }

    @FXML
    void toReturnBook(ActionEvent event) {
        changeContent("lend/returnBook.fxml");
    }

    @FXML
    void toReLend(ActionEvent event) {

    }

    @FXML
    void logout(ActionEvent event) {
        //1 获取主界面
        Stage mainStage = (Stage) this.hlLogout.getScene().getWindow();
        //2 关闭
        mainStage.close();
        //3 创建一个stage
        Stage loginStage = new Stage();
        //4 加载界面布局文件，创建布局以及场景
        try {
            Parent root = FXMLLoader.load(getClass().getResource("/fxml/login.fxml"));
            Scene scene = new Scene(root, 320, 275);
            loginStage.setTitle("MiniLibrary--图书管理系统");
            loginStage.setScene(scene);
            //设置窗口的图标.
            //primaryStage.getIcons().add(new Image(getClass().getResourceAsStream("/images/png-0014.png")));
            //窗口大小不能改变
            loginStage.setResizable(false);
            loginStage.show();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void initialize(URL location, ResourceBundle resources) {
        //1、javafx特有的方式，通过创建线程的方式来加载传递过来的信息。Platform.runLater方法。会用即可
        Platform.runLater(new Runnable() {
            @Override
            public void run() {
                //显示管理员姓名
                lblWelcome.setText("欢迎您，" + loginUser.getLoginName());
            }
        });
        //2、加载显示default.fxml的内容，是一个AnchorPane
        //创建一个AnchorPane,通过读取default.fxml文件创建，并将其加入到 main.fxml中的名字为mainContainer的StackPane中
        changeContent("default.fxml");
    }
}
